package com.goods.business.service.impl;

import com.goods.business.mapper.CategroyMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.CategroyService;
import com.goods.common.error.*;
import com.goods.business.handler.BusinessExceptionHandler;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author XiaoHong
 * @description
 * @date 2022/2/16
 */
@Service
public class CategroyServiceImpl implements CategroyService {
    @Autowired
    private CategroyMapper categroyMapper;
    //加载分类数据
    @Override
    public PageVO<ProductCategoryTreeNodeVO> getCategoryTree(Integer pageNum, Integer pageSize) {
        //查询数据库中的所有数据
        List<ProductCategory> productCategorieList = categroyMapper.selectAll();
        //转换成Vo对象   ：   工具类对所有数据进行多级树形封装的时候需要的参数类型。
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = productCategorieList.stream().map(productCategory ->{
           ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
           BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
           return productCategoryTreeNodeVO;
        }).collect(Collectors.toList());

        //将转换好的对象继续树形封装
        List<ProductCategoryTreeNodeVO>  aaa = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        //将树形封装好的数据进行分页。
        List<ProductCategoryTreeNodeVO> pageList = ListPageUtils.page(aaa, pageSize, pageNum);
        //
        return new PageVO<>(aaa.size(),pageList);
    }

    //加载父级分类数据
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        //查询所有的数据
        List<ProductCategory> productCategorieList = categroyMapper.selectAll();
        //转换成Vo对象   ：   工具类对所有数据进行多级树形封装的时候需要的参数类型。
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = productCategorieList.stream().map(productCategory ->{
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
            return productCategoryTreeNodeVO;
        }).collect(Collectors.toList());

        //将转换好的对象继续树形封装
        List<ProductCategoryTreeNodeVO>  aaa = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        return aaa;
    }

    //添加分类
    @Override
    public void add(ProductCategory productCategory) {
        productCategory.setModifiedTime(new Date());
        productCategory.setCreateTime(new Date());
        categroyMapper.insert(productCategory);
    }

    //点击修改按钮进行显示指定分类数据
    @Override
    public ProductCategory edit(Long id) {
//        ProductCategory productCategory = new ProductCategory();
//        productCategory.setId(id);
        return categroyMapper.selectByPrimaryKey(id);
    }

    //分类数据编辑界面点击修改确认
    @Override
    public void update(ProductCategory productCategory, Long productCategroyId) {
        productCategory.setId(productCategroyId);
        productCategory.setModifiedTime(new Date());
        categroyMapper.updateByPrimaryKey(productCategory);  //不忽略空值
    }

    @Autowired
    private ProductMapper productMapper;
    //删除分类
//    用户点击删除按钮，删除选中数据， 有子分类的、和物资引用的分类不能删除
//    @SneakyThrows
    @Override
    public void delete(Long productCategroyId) throws BusinessException {
        Example example = new Example(ProductCategory.class);
        example.createCriteria().andEqualTo("pid",productCategroyId);
        //1.查询子分类集合
        List<ProductCategory> productCategories = categroyMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(productCategories)){
            //没有子分类：
            //2 判断是否存在物资引用
            Example exampleProduct = new Example(Product.class);
            exampleProduct.createCriteria()
                    .orEqualTo("oneCategoryId",productCategroyId)
                    .orEqualTo("twoCategoryId",productCategroyId)
                    .orEqualTo("threeCategoryId",productCategroyId);
            List<Product> productList = productMapper.selectByExample(exampleProduct);
            if (CollectionUtils.isEmpty(productList)){
                //2 不存在物资引用  进行删除
                categroyMapper.deleteByPrimaryKey(productCategroyId);
            }else{
                throw new BusinessException(BusinessCodeEnum.PRODUCT_OUT_STOCK_EMPTY,"存在物资引用或者子分类");
            }
        }else{
            throw new BusinessException(BusinessCodeEnum.PRODUCT_OUT_STOCK_EMPTY,"存在物资引用或者子分类");
        }

    }

    //
}
